package pms.common.dao;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import org.springframework.core.io.FileSystemResource;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.*;
import pms.common.utils.SessionUtil;
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback;
import org.springframework.jdbc.support.lob.LobCreator;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.util.FileCopyUtils;

import pms.common.value.QaNewValue;

import java.io.File;

public class QaNewDaoImpl implements QaNewDao{
	
	private JdbcTemplate jdbcTemplate;
	private QaNewValue qaNew;
	private LobHandler lobHandler;
	public LobHandler getLobHandler() {
		return lobHandler;
	}
	public void setLobHandler(LobHandler lobHandler) {
		this.lobHandler = lobHandler;
	}
	/** QA_Insert*/
	public void doInsert(final QaNewValue qaNew, final File file, final String fileName)throws 
	      SQLException, InstantiationException, IllegalAccessException,
          ClassNotFoundException, IOException{  
	    Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		final String sysdate = formatter.format(date);
		final String currentUserName = SessionUtil.getUserValue().getUserDispName().trim(); //sessionにユーザーを取り込み
		final byte[] blankValue = new byte[0];
		String l_sqlText = "insert into TBL_QA_LIST(PROJECTID,TEMA,QA_LEVEL,QA_PART,QA_SUMMARY,QA_STATUS,QA_KINKYUU,QA_TO,QA_FROM,QA_QUESTION,QA_ANSWER,QA_START_DATE,QA_LIMIT_DATE,QA_ANSWER_DATE,QA_CONFIRM_DATE,QA_CONFIRMER,QA_FILE_FROM,QA_FILE_FROM_NM,QA_FILE_TO,QA_FILE_TO_NM,QA_MAIL_FLAG,QA_MEMO,QA_DEL_FLAG) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

	    jdbcTemplate.execute(l_sqlText,
	    		new AbstractLobCreatingPreparedStatementCallback(this.lobHandler) {
			protected void setValues(PreparedStatement ps,LobCreator lobCreator)
			throws SQLException{

				ps.setString(1, qaNew.getProjectId());//プロジェクト
				ps.setString(2, qaNew.getTema());//テーマ名
				ps.setString(3, qaNew.getLevel());//段階
				ps.setString(4, qaNew.getPart());//発生箇所
				ps.setString(5, qaNew.getSummary());//概要
				ps.setString(6, "回答待ち");//状態
				ps.setString(7, qaNew.getKinkyuu());//緊急度
				ps.setString(8, qaNew.getAsked());//問合先
				ps.setString(9, currentUserName);//提出者
				ps.setString(10, qaNew.getQuestion());//問題詳細
				ps.setString(11, "");//QA_ANSWER
				ps.setString(12, sysdate);//QA_START_TIME
				ps.setString(13, qaNew.getLimit_date_year()
						+qaNew.getLimit_date_month()+qaNew.getLimit_date_day());//回答期限
				ps.setString(14, "");//QA_ANSWER_DATE
				ps.setString(15, "");//QA_CONFIRMMER_DATE
				ps.setString(16, "");//QA_CONFIRMMER

				if (file == null) {
					lobCreator.setBlobAsBytes(ps, 17, blankValue);//QA_FILE_FROM				
				}else {
					final FileSystemResource res1 = new FileSystemResource(file);
					byte[] mockImg = null;
					try {
						mockImg = FileCopyUtils.copyToByteArray(res1.getFile());
					} catch (IOException e) {
						e.printStackTrace();
					}
					lobCreator.setBlobAsBytes(ps, 17, mockImg);//QA_FILE_FROM
				}
				ps.setString(18, fileName);//QA_FROM_NM
				lobCreator.setBlobAsBytes(ps, 19, blankValue);//QA_FILE_TO
				ps.setString(20, "");//QA_FILE_TO_NM
				ps.setString(21, "0");//QA_MAIL_FLAG
				ps.setString(22, qaNew.getMemo());//QA_MEMO
				ps.setString(23, "0");//QA_DEL_FLAG
				}});
		}

	 public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	 }

	  public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	 }
	 public QaNewValue getQaNew() {
		return qaNew;
	}
	public void setQaNew(QaNewValue qaNew) {
		this.qaNew = qaNew;
	}

}
